package com.yww.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yww.entity.Roles;
import com.yww.entity.Users;
import com.yww.entity.UsersParam;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户 接口
 */

public interface UsersMapper extends BaseMapper<Users>{
    /**
     * 重置用户密码
     * @param
     * @return
     */
    @Update("update users set pass_word='manager' where login_name=#{loginName}")
    boolean updateByUserId(String loginName);

    /**
     * 重置用户密码(根据登录名)
     * @param
     * @return
     */
    @Update("update users set pass_word='guanliyuan' where login_name=#{name}")
    boolean updateByName(String name);


    /**
     * (改)密码错误次数
     * @param loginName
     * @return
     */
    @Update("update users set pas_wrong_num=pas_wrong_num+1 where login_name=#{loginName}")
    Integer updateWrongNum(String loginName);

    /**
     *查询错误次数
     * @param loginName
     * @return
     */
    @Select("select pas_wrong_num from users where login_name=#{loginName}")
    Integer getWrongNum(String loginName);

    //锁定
    @Update("update users set is_lock_out='是' where login_name=#{loginName}")
    Integer updateLock(String loginName);

    //解锁
    @Update("update users set is_lock_out='否' where login_name=#{loginName}")
    Integer updateLockOut(String loginName);

    //清除错误次数
    @Update("update users set pas_wrong_num=0 where login_name=#{loginName}")
    Integer updateEmptyWrongNum(String loginName);

    //查询锁的状态
    @Select("select is_lock_out from users where login_name=#{loginName}")
    String getLock(String loginName);

    //(改)修改用户密码
    @Update("update users set pass_word=#{passWord} where login_name=#{loginName}")
    Integer updatePassWord(Users users);

    //查询用户名
    @Select("select login_name from users where login_name=#{loginName}")
  List<Users> getUserNameByLoginName(Users users);

    //用户签到
    @Update("update users set qd_time=now(),qd_static=1 where login_name=#{loginName}")
    Integer userIn(Users users);
    //用户签退
    @Update("update users set qt_time=now(),qd_static=0 where login_name=#{loginName}")
    Integer userOut(Users users);

    //查询判断用户签到状态
    @Select("select * from users where user_id=#{userId}")
    Users getQdStatic(Users users);

    //查询咨询师角色
   List<Users> getCon(UsersParam usersParam);

   //修改咨询师权重
    @Update("update users set weight=#{weight} where user_id=#{userId}")
    Integer updateWeight(Users users);

    //自动分配
    Integer updateOpenButton(@Param("ids") List<Integer> ids, @Param("num") Integer num);

    //查询用户是否拥有角色
    @Select("select * from user_roles where users_id=#{userId}")
    List<Users> getUserContainsRole(Integer userId);

    //查询角色为"管理员"的用户名
    @Select("select roles.role_name from roles JOIN user_roles on roles.role_id=user_roles.roles_id WHERE user_roles.users_id=#{userId}")
    List<String> getNameByManager(Integer userId);

    //dlushi查询角色为"管理员"的用户名2
    @Select("select * from roles JOIN user_roles on roles.role_id=user_roles.roles_id JOIN users on users.user_id=user_roles.users_id  WHERE roles.role_name LIKE '%管理员%' AND users.login_name=#{loginName} ")
    List<Roles> getNameByName(String loginName);
    //获取最后登录时间
    @Update("update users set last_login_time=#{lastLoginTime} where login_name=#{loginName}")
    Integer getLastTime(@Param("lastLoginTime")String lastLoginTime,@Param("loginName")String loginName);


    //根据用户名查询该用户的密码
    @Select("select * from users where login_name=#{loginName}")
    Users getMimaByName(Users users);

    /**
     * 修改所有人状态为未签到
     * @return
     */
    @Update("update users SET qd_static=0")
    Integer updateStatus();
    /**
     * 修改签到人的状态
     * @return
     */
    @Update(" update users SET qd_static=0 WHERE qd_static=1")
    Integer updateStatus2();

    /**
     * 清空时间
     * @return
     */
    @Update("UPDATE users SET qd_time=null,qt_time=null")
    Integer updateTime();

//    /**
//     * 统计咨询师的学生
//     * @return
//     */
//    @Select("select DISTINCT users.login_name from student join users on student.users_id=users.user_id")
//    List<String> getZxsNum();
//
//    /**
//     * 统计人数
//     * @return
//     */
//    @Select("SELECT COUNT(users_id) FROM student GROUP BY users_id")
//    List<Integer> getPeoples();

//    /*统计图开始*/
//    /**
//     * 统计咨询师的学生
//     * @return
//     */
//    @Select("select DISTINCT users.login_name from student join users on student.users_id=users.user_id")
//    List<String> getZxsNum2();
//
//    /**
//     * 统计人数
//     * @return
//     */
//    @Select("SELECT COUNT(users_id) FROM student where is_pay=1 GROUP BY users_id")
//    List<Integer> getPeoples2();
//
//
//    @Select("select IFNULL(b.num,0) from\n" +
//            "(select users_id from student \n" +
//            " where users_id IS not null\n" +
//            " GROUP BY users_id) as a \n" +
//            " LEFT JOIN\n" +
//            "(select users_id,count(*) num from student \n" +
//            "WHERE is_pay=1 AND users_id IS NOT NULL \n" +
//            "GROUP BY users_id) b on a.users_id=b.users_id")
//    List<Integer> getTokenNums();
    /*统计图结束*/
//清空锁定时间
Integer emptyTime(Integer userId);

    /**
     * 批量删除
     * @param ids
     * @return
     */
    Integer delBatchUserId(String[] ids);
}
